package org.xs.web.http;

import java.io.IOException;

import org.xs.web.http.XSResponse;
import org.xs.web.http.provider.HTTPHeader;
import org.xs.web.http.provider.HTTPResponse;

public class XmlResponse implements XSResponse{

	private String xml;
	private String contentType;
	private int code;
	private HTTPHeader[] headers;
	public XmlResponse(String xml,String contentType,int code,HTTPHeader[] headers){
		this.xml = xml;
		this.contentType = contentType;
		this.code = code;
		this.headers = headers;
	}
	
	public XmlResponse(String xml){
		this(xml,"text/xml; charset=GB2312",200,new HTTPHeader[0]);
	}
	
	public XmlResponse(String xml,String contentType){
		this(xml,contentType,200,new HTTPHeader[0]);
	}
	
	@Override
	public Object apply(HTTPResponse response) {
		response.setStatus(this.code);
		try {
			response.addHeader("Content-Type",this.contentType);
			response.getDelegate().getWriter().write(xml);
		} catch (IOException e) {
			 
		}
		
		return true;
	}

	@Override
	public int getCode() {
		return code;
	}

	@Override
	public String getContentType() {
		return this.contentType;
	}

	@Override
	public HTTPHeader[] getHeaders() {
		return headers;
	}

	@Override
	public boolean isJs() {
		return false;
	}

	@Override
	public boolean isStream() {
		return false;
	}

	@Override
	public boolean isTemplete() {
		return false;
	}

	@Override
	public boolean isText() {
		return false;
	}

	@Override
	public boolean isXml() {
		return true;
	}

}
